# diswasher.r
# Jennifer Kirk 25-07-2009
# Base on a problem from "Digital Dice" by Paul J. Nahin
# Problem 1: The Clumsy Dishwasher Problem
# From p.37
# Use a simulation to estimate the probability that a particular
# dishwasher at a restaurant breaks at least four of the five
# broken dishes in a week, assuming all five dishwashers at the restaurant
# have same probability of breaking a dish.

# this is my solution, which assumes that A is the dishwasher we are interested in

#dishwashers is the list of the 5 dishwashers
dishwashers <-c("A","B","C","D","E")

#broken is a vector that will contain the list of who broken each dish in a give week
broken <- c()

#dishes is the weekly count for the dishwasher we are interested in
dishes <- 0

#times is the number of times our dishwasher has broken 4 or more dishes
times <- 0

for (i in 1:100000)
{

	# this samples the list of 5 dishwahsers with replacement and stores the sample
	# in the vector broken
	broken <- sample(dishwashers, replace = TRUE)

	for (j in 1:length(broken))
	{
		if (broken[j] == "A") dishes <- dishes + 1
	}

	if (dishes >= 4) times <- times + 1
	dishes <- 0
}

print(times/100000)
